package com.annotation.test;

import com.algomypractice.sort.timecomplexityon2.BubbleSort;
import com.algomypractice.sort.timecomplexityon2.InsertionSort;
import com.annotation.service.SortTest;
import com.utils.ClassUtils;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * @author: ZhouBert
 * @date: 2019/6/12
 * @description: 排序测试工厂
 */
public class SortTestFactory {
	static int size = 100000;

	public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, IOException, ClassNotFoundException {

		List<Class> lists = new ArrayList<>();
		//region	集成排序类集合
		BubbleSort bubbleSort = new BubbleSort();
		Class<? extends BubbleSort> bubbleClass = bubbleSort.getClass();
		lists.add(bubbleClass);
		InsertionSort insertionSort = new InsertionSort();
		Class<? extends InsertionSort> insertionSortClass = insertionSort.getClass();
		lists.add(insertionSortClass);

		//endregion


		List<Class> classes = ClassUtils.getClasses("com.algomypractice.sort.timecomplexityon2");

		//region 生成随机数组
		int[] arr = new int[size];
		Random random = new Random();
		for (int i = 0; i < size; i++) {
			arr[i] = random.nextInt(size);
		}
		//endregion

		//region 准备测试参数
		List<int[]> list = new ArrayList<>();
		int len = classes.size() * 3;
		for (int i = 0; i < len; i++) {
			list.add(Arrays.copyOf(arr, arr.length));
		}

		int i = 0;
		//endregion

		for (Class cls :
				classes) {
			Method[] methods = cls.getDeclaredMethods();
			for (Method method :
					methods) {
				if (method.isAnnotationPresent(SortTest.class)) {
					method.setAccessible(true);
					System.out.println(method.getName());
					long time1 = System.currentTimeMillis();
					method.invoke(cls, list.get(i++));
					System.out.println(System.currentTimeMillis() - time1 + "ms");

				}
			}
		}


	}


}
